<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="学生证" prop="image">
      <!--   <el-input v-model="dataForm.image" placeholder="学生证"></el-input> -->
        <single-upload v-model="dataForm.image"></single-upload>
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <!--       <el-input v-model="dataForm.status" placeholder="审核状态"></el-input> -->
        <el-switch
          v-model="dataForm.status"
          :active-value="1"
          :inactive-value="0"
          active-color="#13ce66"
          inactive-color="#ff4949"
        >
        </el-switch>
      </el-form-item>

      <el-form-item label="选择用户">
        <div>
          <el-cascader
            style="width: 85%"
            filterabl
            clearable
            placeholder="试试搜索:用户名"
            v-model="paths"
            :options="userlist"
            :props="setting"
          ></el-cascader>
        </div>
      </el-form-item>
      <el-form-item label="身份证号" prop="cardNum">
        <el-input v-model="dataForm.cardNum" placeholder="身份证号"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import singleUpload from "@/components/upload/singleUpload";
export default {
    components: { singleUpload } /* 注册进来 */,
  data() {
    return {
      userlist: [],
      setting: {
        value: "id",
        label: "name",
      },
      paths: [],
      visible: false,
      dataForm: {
        id: 0,
        name: "",
        image: "",
        status: "",
        deleted: "",
        userId: "",
        cardNum: "",
      },
      dataRule: {
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        image: [{ required: true, message: "学生证不能为空", trigger: "blur" }],
        status: [
          { required: true, message: "审核状态不能为空", trigger: "blur" },
        ],
        deleted: [
          { required: true, message: "逻辑删除不能为空", trigger: "blur" },
        ],
        userId: [
          { required: true, message: "用户的id不能为空", trigger: "blur" },
        ],
        cardNum: [
          { required: true, message: "身份证号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //获取全部用户信息
    getUserList() {
      this.$http({
        url: this.$http.adornUrl("/help/user/findAll"),
        method: "get",
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.userlist = data.data;
        }
      });
    },
    init(id) {
      this.getUserList();
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/help/approve/info/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.approve.name;
              this.dataForm.image = data.approve.image;
              this.dataForm.status = data.approve.status;
              this.dataForm.deleted = data.approve.deleted;
              this.dataForm.userId = data.approve.userId;
              this.dataForm.cardNum = data.approve.cardNum;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/help/approve/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              image: this.dataForm.image,
              status: this.dataForm.status,
              deleted: this.dataForm.deleted,
              userId: this.paths[0],
              cardNum: this.dataForm.cardNum,
            }),
          }).then(({ data }) => {
            this.paths = [];
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
